package book.ioperations;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperations implements IOperations{
    public void work(BookList bookList){
        System.out.println("新增图书...");

        int currentsize = bookList.getUsedSize();
        if(currentsize == bookList.getBooks().length){
            System.out.println("书架已满，新增失败");
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入书名：");
        String name = scanner.nextLine();
        System.out.println("请输入作者名：");
        String author = scanner.nextLine();
        System.out.println("请输入价格：");
        int price = scanner.nextInt();
        System.out.println("请输入书籍类型：");
        String type = scanner.nextLine();

        Book newbook = new Book(name,author,price,type);

        for (int i = 0; i < currentsize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(name)) {
                System.out.println("此书已存在，插入失败");
                return;
            }

            bookList.setBook(currentsize,newbook);
            bookList.setUsedSize(currentsize++);

            System.out.println("新增图书成功！");
        }
    }
}
